package de.metalcon.gwt.client.rpc;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

import de.metalcon.gwt.shared.ContentTransmitter;

public class ContentTransmitterHandler {
	// private static HashMap<String, AContentTransmitter> list = new
	// HashMap<String, AContentTransmitter>(); HashMap gibts leider noch nicht
	// in JS
	private static HashMap<String, ContentTransmitter> list = new HashMap<String, ContentTransmitter>();
	private final static ContentServiceAsync greetingService = GWT.create(ContentService.class);

	public static void getTransmitter(String moduleName, String uniqueKey, HashMap<String, Object> requestVars, AsyncTransmitterCallback<ContentTransmitter> callback) {
		ContentTransmitter node = null;
		if (uniqueKey != null) {
			node = getContentTransmitterIfCached(uniqueKey);
		}
		if (node != null) {
			callback.afterSuccess(node);
			Window.alert(node.getUniqueKey() + " aus ContentTransmitterHandler geholt");
		} else {
			greetingService.getContentTransmitter(moduleName, requestVars, callback);
		}
	}

	public static ContentTransmitter getContentTransmitterIfCached(String key) {
		ContentTransmitter t = list.get(key);
		if (t != null) {
			return t;
		}
		return null;
	}

	public static boolean isNodeDirty(String key) {
		ContentTransmitter t = list.get(key);
		if (t != null) {
			return t.isDirty();
		}
		return true;
	}

	public static void updateTransmitter(ContentTransmitter node) {
		list.put(node.getUniqueKey(), node);
	}
}
